package cn.net.xyan.easy.spring.jdbc.utils;

import org.hsqldb.server.Server;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

/**
 * Created by zarra on 15/4/2.
 */
public class DataBaseServer {
    private String dbDriver = "org.hsqldb.jdbcDriver";
    private String dbPath;
    private String dbName;
    private int port;
    private boolean silent;
    private Server server;

    Logger logger = LoggerFactory.getLogger(DataBaseServer.class);

    public DataBaseServer(){
        server = new Server();
    }

    public String getDbPath() {
        return dbPath;
    }

    public void setDbPath(String dbPath) {
        this.dbPath = dbPath;
    }

    public String getDbName() {
        return dbName;
    }

    public void setDbName(String dbName) {
        this.dbName = dbName;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public boolean isSilent() {
        return silent;
    }

    public void setSilent(boolean silent) {
        this.silent = silent;
    }

    public String getDbDriver() {
        return dbDriver;
    }

    public void setDbDriver(String dbDriver) {
        this.dbDriver = dbDriver;
    }

    @PostConstruct
    public void init(){
        server.setDatabaseName(0,dbName);
        server.setDatabasePath(0, dbPath + "/" + dbName);
        server.setPort(port);
        server.setSilent(isSilent());
        server.start();
        logger.info("hsql server start!!!!!!!");
    }

    @PreDestroy
    public void destroy(){
        server.stop();
        logger.info("hsql server stop!!!!!!!");
    }

    public String getConnectURL(){
        String v = "jdbc:hsqldb:hsql://127.0.0.1:"+port+"/"+dbName+";shutdown=false";
        return v;
    }

    public Server getServer() {
        return server;
    }
}
